//你好
const core = String.raw/* cpp */`

  //OBJECT_READ_WRITE

  //JS_LIMB_BITS
  //mp_sub


  /* binary object writer & reader */

  //#pragma region MySection

  //#pragma endregion


  // #define HEAP_SIZE  (256 * 1024)
  // static uint8_t heap_area[HEAP_SIZE];
  extern tlsf_t tlsf;

  typedef struct {
      size_t free_size;
  } free_info_t;

  /* 回调函数，用于统计空闲块 */
  static void pool_walker(void* ptr, size_t size, int used, void* user)
  {
      free_info_t* info = (free_info_t*)user;
      if (!used) {
        info->free_size += size;
        //printf("f:%d %08X\n", size, (int)ptr);
      }else{
        //printf("u:%d %08X\n", size, (int)ptr);
      }
  }

  /* 获取 TLSF 剩余可用空间 */
  size_t tlsf_get_free_size()
  {
      free_info_t info = {0};
      tlsf_walk_pool(tlsf_get_pool(tlsf), pool_walker, &info);
      //printf("----\n");
      return info.free_size;
  }
  // void *my_malloc(size_t size) {
  //     return tlsf_malloc(tlsf, size);
  // }

  // void my_free(void *p) {
  //     tlsf_free(tlsf, p);
  // }

  // void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);


  int malloc_count;
  int malloc_size;

  #define MALLOC_OVERHEAD  8

  /* default memory allocation functions with memory limitation */
  static size_t js_def_malloc_usable_size(const void *ptr)
  {
      return tlsf_block_size((void *)ptr);
  }

  static void *js_def_malloc(JSMallocState *s, size_t size)
  {
      void *ptr;

      /* Do not allocate zero bytes: behavior is platform dependent */
      //assert(size != 0);

      if (s->malloc_size + size > s->malloc_limit)
          return 0;

      ptr = tlsf_malloc(tlsf, size);//malloc(size);
      if (!ptr)
          return 0;

      s->malloc_count++;
      s->malloc_size += tlsf_block_size(ptr) + MALLOC_OVERHEAD;
      
      malloc_count = s->malloc_count;
      malloc_size = s->malloc_size;
      return ptr;
  }

  static void js_def_free(JSMallocState *s, void *ptr)
  {
      if (!ptr)
          return;

      s->malloc_count--;
      s->malloc_size -= tlsf_block_size(ptr) + MALLOC_OVERHEAD;
      tlsf_free(tlsf, ptr);
      malloc_count = s->malloc_count;
      malloc_size = s->malloc_size;
  }

  static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
  {
      size_t old_size;

      if (!ptr) {
          if (size == 0)
              return 0;
          return js_def_malloc(s, size);
      }
      old_size = tlsf_block_size(ptr);
      if (size == 0) {
          s->malloc_count--;
          s->malloc_size -= old_size + MALLOC_OVERHEAD;
          tlsf_free(tlsf, ptr);
          return 0;
      }
      if (s->malloc_size + size - old_size > s->malloc_limit)
          return 0;

      ptr = tlsf_realloc(tlsf, ptr, size);
      if (!ptr)
          return 0;

      s->malloc_size += tlsf_block_size(ptr) - old_size;
      
      malloc_count = s->malloc_count;
      malloc_size = s->malloc_size;

      return ptr;
  }

  static const JSMallocFunctions def_malloc_funcs = {
      js_def_malloc,
      js_def_free,
      js_def_realloc,
      js_def_malloc_usable_size,
  };

  // 打印 JS 错误
  static void dump_exception(JSContext *ctx, JSValue v) {
      const char *str = JS_ToCString(ctx, v);
      if (str) {
          printf("Exception: %s\r\n", str);
          JS_FreeCString(ctx, str);
      } else {
          printf("Exception (unknown)\r\n");
      }
  }









  typedef struct Timer {
      int id;
      uint64_t expire_ms;
      uint64_t interval_ms;
      int repeat;
      JSValue callback;
      struct Timer *next;
  } Timer;

  static Timer *timer_list = 0;
  static int next_timer_id = 1;

  // 添加定时器
  static int add_timer(JSContext *ctx, JSValueConst func, int delay, int repeat) {
      Timer *t = (Timer*)malloc(sizeof(Timer));
      t->id = next_timer_id++;
      t->expire_ms = getMills() + delay;
      t->interval_ms = delay;
      t->repeat = repeat;
      t->callback = JS_DupValue(ctx, func);
      t->next = timer_list;
      timer_list = t;
      return t->id;
  }

  // 删除定时器
  static void remove_timer(JSContext *ctx, int id) {
      Timer **pp = &timer_list;
      while (*pp) {
          Timer *t = *pp;
          if (t->id == id) {
              *pp = t->next;
              JS_FreeValue(ctx, t->callback);
              free(t);
              return;
          }
          pp = &t->next;
      }
  }

  // C 层 setTimeout(func, delay)
  static JSValue js_setTimeout(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
      if (argc < 2 || !JS_IsFunction(ctx, argv[0])){
          return JS_UNDEFINED;
      }
      int delay = 0;
      JS_ToInt32(ctx, &delay, argv[1]);
      uint32_t id = add_timer(ctx, argv[0], delay, 0);
      return JS_NewUint32(ctx, id);
  }

  // C 层 setInterval(func, delay)
  static JSValue js_setInterval(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
      if (argc < 2 || !JS_IsFunction(ctx, argv[0])){
          return JS_UNDEFINED;
      }
      int delay = 0;
      JS_ToInt32(ctx, &delay, argv[1]);
      uint32_t id = add_timer(ctx, argv[0], delay, 1);
      return JS_NewUint32(ctx, id);
  }

  // C 层 clearTimeout / clearInterval
  static JSValue js_clearTimer(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
      int id = 0;
      JS_ToInt32(ctx, &id, argv[0]);
      remove_timer(ctx, (uint32_t)id);
      return JS_UNDEFINED;
  }

  // 检查所有 timer，执行到期的
  void check_timers(JSContext *ctx) {
      uint64_t now = getMills();
      Timer *t = timer_list;
      Timer *prev = 0;

      while (t) {
        if (now >= t->expire_ms) {
          // 执行 JS 回调
          JSValue ret = JS_Call(ctx, t->callback, JS_UNDEFINED, 0, 0);
          if (JS_IsException(ret)) {
              dump_exception(ctx, JS_GetException(ctx));
          }
          JS_FreeValue(ctx, ret);

          if (t->repeat) {
              t->expire_ms = t->expire_ms + t->interval_ms;
              prev = t;
              t = t->next;
          } else {
              Timer *to_free = t;
              if (prev)
                  prev->next = t->next;
              else
                  timer_list = t->next;
              t = t->next;
              JS_FreeValue(ctx, to_free->callback);
              free(to_free);
          }
        } else {
          prev = t;
          t = t->next;
        }
      }
  }


  void js_init(){
      malloc_count = 0;
      malloc_size = 0;
      // 创建运行时和上下文
      rt = JS_NewRuntime(&def_malloc_funcs, 0);
      if (!rt) {
          printf("Failed to create JSRuntime\r\n");
          abort();
      }
      ctx = JS_NewContext(rt);
      if (!ctx) {
          printf("Failed to create JSContext\r\n");
          JS_FreeRuntime(rt);
          abort();
      }

      //setTimeout
      //setInterval
      
      global = JS_GetGlobalObject(ctx);
      JS_SetPropertyStr(ctx, global, "setTimeout", JS_NewCFunction(ctx, js_setTimeout, "setTimeout", 2));
      JS_SetPropertyStr(ctx, global, "setInterval", JS_NewCFunction(ctx, js_setInterval, "setInterval", 2));
      JS_SetPropertyStr(ctx, global, "clearTimeout", JS_NewCFunction(ctx, js_clearTimer, "clearTimeout", 1));
      JS_SetPropertyStr(ctx, global, "clearInterval", JS_NewCFunction(ctx, js_clearTimer, "clearInterval", 1));

  }

  void exec(const char* script){
    JSValue val = JS_Eval(ctx, script, strlen(script), "<input>", JS_EVAL_TYPE_GLOBAL);
    if (JS_IsException(val)) {
        dump_exception(ctx, JS_GetException(ctx));
    }
    JS_FreeValue(ctx, val);
  }
`;








const time = String.raw/* cpp */`

  #include <stdio.h>
  #include <stdint.h>

  typedef struct {
      int year, month, day;
      int hour, minute, second, millisecond;
  } DateTime;

  // 判断闰年
  int is_leap(int year) {
      return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  }

  // 毫秒数转日期
  DateTime ms_to_datetime(uint64_t ms) {
      DateTime dt;
      uint64_t total_seconds = ms / 1000;
      dt.millisecond = ms % 1000;

      dt.second = total_seconds % 60;
      uint64_t total_minutes = total_seconds / 60;
      dt.minute = total_minutes % 60;
      uint64_t total_hours = total_minutes / 60;
      dt.hour = total_hours % 24;
      uint64_t total_days = total_hours / 24;

      // 计算年份
      int year = 1970;
      while (1) {
          int days_in_year = is_leap(year) ? 366 : 365;
          if (total_days >= days_in_year) {
              total_days -= days_in_year;
              year++;
          } else {
              break;
          }
      }
      dt.year = year;

      // 每个月天数
      int days_in_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
      if (is_leap(year)) days_in_month[1] = 29;

      int month = 0;
      while (total_days >= days_in_month[month]) {
          total_days -= days_in_month[month];
          month++;
      }
      dt.month = month + 1;  // 月份从1开始
      dt.day = total_days + 1; // 天从1开始

      return dt;
  }

  int main() {
      uint64_t ms = 1700000000123ULL; // 毫秒数
      DateTime dt = ms_to_datetime(ms);

      printf("%04d-%02d-%02d %02d:%02d:%02d.%03d\n",
            dt.year, dt.month, dt.day,
            dt.hour, dt.minute, dt.second, dt.millisecond);

      return 0;
  }





  #include <stdio.h>
#include <stdint.h>

typedef struct {
    int year, month, day;
    int hour, minute, second, millisecond;
} DateTime;

// 判断闰年
int is_leap(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// 每个月的天数
int days_in_month(int year, int month) {
    int dim[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    if (month == 2 && is_leap(year)) return 29;
    return dim[month - 1];
}

// DateTime 转毫秒数
uint64_t datetime_to_ms(DateTime dt) {
    uint64_t total_days = 0;

    // 计算从1970年到目标年份的天数
    for (int y = 1970; y < dt.year; y++) {
        total_days += is_leap(y) ? 366 : 365;
    }

    // 计算本年到目标月份的天数
    for (int m = 1; m < dt.month; m++) {
        total_days += days_in_month(dt.year, m);
    }

    // 加上本月的天数（减1，因为当天还没结束）
    total_days += dt.day - 1;

    // 转换成毫秒
    uint64_t ms = total_days * 24ULL * 60 * 60 * 1000;
    ms += dt.hour * 60ULL * 60 * 1000;
    ms += dt.minute * 60ULL * 1000;
    ms += dt.second * 1000ULL;
    ms += dt.millisecond;

    return ms;
}

int main() {
    DateTime dt = {2024, 11, 14, 20, 26, 40, 123};
    uint64_t ms = datetime_to_ms(dt);
    printf("毫秒数: %llu\n", ms);
    return 0;
}







`;












module.exports = { core };